package m1.server.config;

import m1.server.config.component.ConnectionManager;
import m1.server.config.component.Database;
import m1.server.config.component.SecurityManager;
import m1.server.config.connector.ClearanceRequest;
import m1.server.config.connector.SQLQuery;
import m1.server.config.connector.SecurityRequest;
import m2.links.Attachment;
import m2.other.Config;

public class ServerConf extends Config{

	private static ServerConf serverConf;
	private ConnectionManager connectionManager = ConnectionManager.getInstance();
	private ClearanceRequest clearanceRequest = ClearanceRequest.getInstance();
	private SQLQuery sqlQuery = SQLQuery.getInstance();
	private Database database = Database.getInstance();
	private SecurityManager securityManager = SecurityManager.getInstance();
	private SecurityRequest securityRequest = SecurityRequest.getInstance();
	private Attachment securCheckReceivRequest;
	private Attachment requestSecurityCheck;
	private Attachment dbQueryReceivCallee;
	private Attachment receiveCalleeClientRPC;
	private Attachment queryRecCalleeDBSQL;
	private Attachment queryCallerDBSQL;
	private Attachment securManagRequestorDBSecurReq;
	private Attachment securManResReqDBSecurReq;
	private Attachment credQuerSendSecuManagRequ;
	private Attachment credQuerSecuManagRequ;
	private Attachment secuAuthGranSecuManClear;
	private Attachment secuAuthSendGranSecuManClear;
	
	/**
	 * 
	 * @return
	 */
	public static ServerConf getInstance(){
		if (serverConf == null){
			serverConf = new ServerConf();
		}
		return serverConf;
	}
	
	/**
	 * 
	 */
	private ServerConf(){		
		
		clearanceRequest = ClearanceRequest.getInstance();
		sqlQuery = SQLQuery.getInstance();
		database = Database.getInstance();
		securityManager = SecurityManager.getInstance();
		securityRequest = SecurityRequest.getInstance();
		
		ServerConfInterfaceProvided serverConfInterfaceProvided = new ServerConfInterfaceProvided();
		ServerConfInterfaceRequired serverConfInterfaceRequired = new ServerConfInterfaceRequired();
		addComponentInterfaceProvided(serverConfInterfaceProvided);
		addComponentInterfaceRequired(serverConfInterfaceRequired);
		addProvidedPort(new ConfOut("ConfOut"));
		addRequiredPort(new ConfIn("ConfIn"));
		addComponent(securityManager, database, connectionManager);	
		
		serverConfInterfaceProvided.buildService();
		serverConfInterfaceRequired.buildService();
		
		//*******ConnectionManager*****
		//ConnectionManager-ClearanceRequest
		securCheckReceivRequest = new Attachment();
		securCheckReceivRequest.connectRoleToPort(clearanceRequest.getProvidedRole("ReceiveRequestorSecurityCheck"),
				connectionManager.getRequiredPort("SecurityCheckReceive"));
	
		requestSecurityCheck = new Attachment();
		requestSecurityCheck.connectPortToRole(connectionManager.getProvidedPort("SecurityCheck"),
				clearanceRequest.getRequiredRole("RequestorSecurityCheck"));
	
		//ConnectionManager-SQLQuery
		dbQueryReceivCallee = new Attachment();
		dbQueryReceivCallee.connectRoleToPort(sqlQuery.getProvidedRole("CalleeDBQuery"),
				connectionManager.getRequiredPort("DBQueryReceive"));
	
		receiveCalleeClientRPC = new Attachment();
		receiveCalleeClientRPC.connectPortToRole(connectionManager.getProvidedPort("DBQuery"),
				sqlQuery.getRequiredRole("CallerDBQuery"));
	
		//*******Database*****
		//Database-SQLQuery
		queryRecCalleeDBSQL = new Attachment();
		queryRecCalleeDBSQL.connectRoleToPort(sqlQuery.getProvidedRole("CalleeQueryReceive"),
				database.getRequiredPort("QueryReceive"));
	
		queryCallerDBSQL = new Attachment();
		queryCallerDBSQL.connectPortToRole(database.getProvidedPort("Query"),
				sqlQuery.getRequiredRole("CallerQuery"));
	
		//Database-SecurityRequest
		securManagRequestorDBSecurReq = new Attachment();
		securManagRequestorDBSecurReq.connectRoleToPort(securityRequest.getProvidedRole("RequestorSecurityManagement"),
				database.getRequiredPort("SecurityManagement"));
	
		securManResReqDBSecurReq = new Attachment();
		securManResReqDBSecurReq.connectPortToRole(database.getProvidedPort("SecurityManagementReceive"),
				securityRequest.getRequiredRole("ReceiveRequestorSecurityManagement"));
	
		//*******SecurityManager*****
		//SecurityManager-SecurityRequest
		credQuerSendSecuManagRequ = new Attachment();
		credQuerSendSecuManagRequ.connectRoleToPort(securityRequest.getProvidedRole("SendSecurity"),
				securityManager.getRequiredPort("CredentialQuerySend"));
	
		credQuerSecuManagRequ = new Attachment();
		credQuerSecuManagRequ.connectPortToRole(securityManager.getProvidedPort("CredentialQuery"),
				securityRequest.getRequiredRole("Security"));
	
		////SecurityManager-ClearanceRequest
		secuAuthGranSecuManClear = new Attachment();
		secuAuthGranSecuManClear.connectRoleToPort(clearanceRequest.getProvidedRole("Grantor"),
				securityManager.getRequiredPort("SecurityAuthorization"));
		
		secuAuthSendGranSecuManClear = new Attachment();
		secuAuthSendGranSecuManClear.connectPortToRole(securityManager.getProvidedPort("SecurityAuthorizationSend"),
				clearanceRequest.getRequiredRole("SendGrantor"));	
	}
	
	/**
	 * 
	 * @return
	 */
	public ConnectionManager getConnectionManager() {
		return connectionManager;
	}

	/**
	 * 
	 * @return
	 */
	public ClearanceRequest getClearanceRequest() {
		return clearanceRequest;
	}

	/**
	 * 
	 * @return
	 */
	public SQLQuery getSqlQuery() {
		return sqlQuery;
	}

	/**
	 * 
	 * @return
	 */
	public Database getDatabase() {
		return database;
	}

	/**
	 * 
	 * @return
	 */
	public SecurityManager getSecurityManager() {
		return securityManager;
	}

	/**
	 * 
	 * @return
	 */
	public SecurityRequest getSecurityRequest() {
		return securityRequest;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getSecurCheckReceivRequest() {
		return securCheckReceivRequest;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getRequestSecurityCheck() {
		return requestSecurityCheck;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getDbQueryReceivCallee() {
		return dbQueryReceivCallee;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getReceiveCalleeClientRPC() {
		return receiveCalleeClientRPC;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getQueryRecCalleeDBSQL() {
		return queryRecCalleeDBSQL;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getQueryCallerDBSQL() {
		return queryCallerDBSQL;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getSecurManagRequestorDBSecurReq() {
		return securManagRequestorDBSecurReq;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getSecurManResReqDBSecurReq() {
		return securManResReqDBSecurReq;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getCredQuerSendSecuManagRequ() {
		return credQuerSendSecuManagRequ;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getCredQuerSecuManagRequ() {
		return credQuerSecuManagRequ;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getSecuAuthGranSecuManClear() {
		return secuAuthGranSecuManClear;
	}

	/**
	 * 
	 * @return
	 */
	public Attachment getSecuAuthSendGranSecuManClear() {
		return secuAuthSendGranSecuManClear;
	}
	
}
